<?php

namespace app\api\model;

use think\Model;

class UserList extends Model
{
    public static function getUserInfoByDeviceId($deviceId)
    {
        return self::where(['device_id' => $deviceId])->find();
    }

    public static function getUserInfoByUserId($userId)
    {
        return self::where(['id' => $userId])->find();
    }

    public static function createUserToken($userId, $deviceId)
    {
        return aesEncrypt(json_encode([
            'user_id' => $userId,
            'device_id' => $deviceId,
            'expires_in' => time() + 86400 * 7
        ]), 'yytk');
    }

    public static function createUserInfoByDeviceId($deviceId)
    {
        $userId = self::insertGetId([
            'name' => '游客登录',
            'avatar' => '/upload/image/20250624/1169a36a05f9a573a9bddb96d5e95fe5.jpg',
            'sign' => uniqid(),
            'device_id' => $deviceId,
            'create_time' => time(),
            'update_time' => time(),
        ]);
        return self::getUserInfoByUserId($userId);
    }

    public static function updateUserChallenge($userId, $challenge)
    {
        return self::where(['id' => $userId])->update(['challenge_day' => $challenge]);
    }
}